<?
/*
 *  Jiba: A small collection of classes to make php easier.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * timer.class.php, Jiba
 * 
 * A simple timer to messure page execution time (or something else).
 * It can handle multiple measurements and intermission times.   
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package jiba
 */
class Timer
{
    var $_startTime = array();  //private
    var $_endTime   = array();  //private
    var $_interTime = array();  //private
    
    /**
     * Constructor function: automaticly starts
     * the def timer.
     *
     * @param   bool    $start      if true the timer starts
     * @param   string  $id         the id of the timer to start
     * @return  void
     * @access  public
     */
    function timer( $start=true, $id='def' )
    {        
        if( $start ) $this->start( $id );
    }
    
    /**
     * Start a new timer
     *
     * @param   string  $id         the id of the timer to start
     * @return  void
     * @access  public
     */
    function start( $id='def' )
    {
        $this->_startTime[$id] = microtime( true );
    }
    
    /**
     * Stop a running timer
     *
     * @param   string  $id         the id of the timer to stop
     * @return  void
     * @access  public
     */
    function stop( $id='def' )
    {
        if( isset($this->_startTime[$id]) )
            $this->_endTime[$id] = microtime( true );
    }
    
    /**
     * Make e new intermission time
     * 
     * @param   string      $id         the id of the timer for the intermission
     * @param   string|int  $interId    the id of the intermission (blank will use the next array index)
     * @return  void
     * @access  public
     */
    function newInter( $id='def', $interId='' )
    {
        if( empty($interId) )
            $this->_interTime[$id][$interId] = microtime( true );
        else
            $this->_interTime[$id][] = microtime( true );
    }
    
    /**
     * Get the time meassured by a timer
     *
     * @param   string  $type   us, ms, s, m for micorseconds, milliseconds, seconds or minutes
     * @param   int     $dec    numbers behind the decimalmark
     * @param   string  $id     id of the timer
     */
    function getTime( $type='s', $dec=3, $id='def' )
    {
        if( !isset($this->_endTime[$id]) )
        {
            exit;
        }
        elseif( !isset($this->_startTime[$id]) )
        {
            exit;
        }
        else
            return $this->_formatTime( $type, $dec, $this->_endTime[$id] - $this->_startTime[$id] );
    }
    
    /**
     * Get the time between the start of the timer and the intermission time
     *
     * @param   string      $type       us, ms, s, m for micorseconds, milliseconds, seconds or minutes
     * @param   int         $dec        numbers behind the decimalmark
     * @param   int|string  $interId    the intermission time to use
     * @param   string      $id         the id of the timer
     * @return  int         the time between the start of the timer and the intermission time
     * @access  public
     */
    function getInter( $type='s', $dec=3, $interId, $id='def' )
    {
        if( isset($this->_interTime[$id][$inter]) )
            return $this->_formatTime( $type, $dec, $this->_interTime[$id][$inter] - $this->_startTime[$id] );
        elseif( !isset($this->_startTime[$id]) )
        {
            exit;
        }
        else
        {
            exit;
        }
    }
    
    /**
     * Turns a time in seconds into another format (s, minutes, ms, ...)
     * 
     * @param   string  $type   format to output us, ms, s, m, h
     * @param   int     $dec    numbers behind the decimalmark
     * @param   int     $time   the time to format in seconds
     * @return  int     the formated time
     * @access  private
     */
    function _formatTime( $type, $dec, $time )
    {
        switch( $type )
        {
            case 'us': 
                $time*=1000000;
                break;
            case 'ms':
                $time*=1000;
                break;
            case 's': //the time is allready in s, so we do nothing
                break;
            case 'm':
                $time = $time/60;
                break;
            case 'h':
                $time = $time/60/60;
                break;
            default:
                break;
        }
        
        return round( $time, $dec );
    }
    
}
?>